<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>26.3. unittest — Unit testing framework &mdash; Python v2.6.2 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2.6.2',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v2.6.2 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v2.6.2 documentation" href="../index.html" />
    <link rel="up" title="26. Development Tools" href="development.html" />
    <link rel="next" title="26.4. 2to3 - Automated Python 2 to 3 code translation" href="2to3.html" />
    <link rel="prev" title="26.2. doctest — Test interactive Python examples" href="doctest.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 

  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             accesskey="M">modules</a> |</li>
        <li class="right" >
          <a href="2to3.html" title="26.4. 2to3 - Automated Python 2 to 3 code translation"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="doctest.html" title="26.2. doctest — Test interactive Python examples"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="development.html" accesskey="U">26. Development Tools</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-unittest">
<h1>26.3. <tt class="xref docutils literal"><span class="pre">unittest</span></tt> &#8212; Unit testing framework<a class="headerlink" href="#module-unittest" title="Permalink to this headline">¶</a></h1>
<p>
<span class="versionmodified">New in version 2.1.</span></p>
<p>The Python unit testing framework, sometimes referred to as &#8220;PyUnit,&#8221; is a
Python language version of JUnit, by Kent Beck and Erich Gamma. JUnit is, in
turn, a Java version of Kent&#8217;s Smalltalk testing framework.  Each is the de
facto standard unit testing framework for its respective language.</p>
<p><tt class="xref docutils literal"><span class="pre">unittest</span></tt> supports test automation, sharing of setup and shutdown code for
tests, aggregation of tests into collections, and independence of the tests from
the reporting framework.  The <tt class="xref docutils literal"><span class="pre">unittest</span></tt> module provides classes that make
it easy to support these qualities for a set of tests.</p>
<p>To achieve this, <tt class="xref docutils literal"><span class="pre">unittest</span></tt> supports some important concepts:</p>
<dl class="docutils">
<dt>test fixture</dt>
<dd>A <em>test fixture</em> represents the preparation needed to perform one or more
tests, and any associate cleanup actions.  This may involve, for example,
creating temporary or proxy databases, directories, or starting a server
process.</dd>
<dt>test case</dt>
<dd>A <em>test case</em> is the smallest unit of testing.  It checks for a specific
response to a particular set of inputs.  <tt class="xref docutils literal"><span class="pre">unittest</span></tt> provides a base class,
<a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a>, which may be used to create new test cases.</dd>
<dt>test suite</dt>
<dd>A <em>test suite</em> is a collection of test cases, test suites, or both.  It is
used to aggregate tests that should be executed together.</dd>
<dt>test runner</dt>
<dd>A <em>test runner</em> is a component which orchestrates the execution of tests
and provides the outcome to the user.  The runner may use a graphical interface,
a textual interface, or return a special value to indicate the results of
executing the tests.</dd>
</dl>
<p>The test case and test fixture concepts are supported through the
<a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> and <a title="unittest.FunctionTestCase" class="reference internal" href="#unittest.FunctionTestCase"><tt class="xref docutils literal"><span class="pre">FunctionTestCase</span></tt></a> classes; the former should be
used when creating new tests, and the latter can be used when integrating
existing test code with a <tt class="xref docutils literal"><span class="pre">unittest</span></tt>-driven framework. When building test
fixtures using <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a>, the <tt class="xref docutils literal"><span class="pre">setUp()</span></tt> and <tt class="xref docutils literal"><span class="pre">tearDown()</span></tt> methods
can be overridden to provide initialization and cleanup for the fixture.  With
<a title="unittest.FunctionTestCase" class="reference internal" href="#unittest.FunctionTestCase"><tt class="xref docutils literal"><span class="pre">FunctionTestCase</span></tt></a>, existing functions can be passed to the constructor
for these purposes.  When the test is run, the fixture initialization is run
first; if it succeeds, the cleanup method is run after the test has been
executed, regardless of the outcome of the test.  Each instance of the
<a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> will only be used to run a single test method, so a new
fixture is created for each test.</p>
<p>Test suites are implemented by the <a title="unittest.TestSuite" class="reference internal" href="#unittest.TestSuite"><tt class="xref docutils literal"><span class="pre">TestSuite</span></tt></a> class.  This class allows
individual tests and test suites to be aggregated; when the suite is executed,
all tests added directly to the suite and in &#8220;child&#8221; test suites are run.</p>
<p>A test runner is an object that provides a single method, <tt class="xref docutils literal"><span class="pre">run()</span></tt>, which
accepts a <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> or <a title="unittest.TestSuite" class="reference internal" href="#unittest.TestSuite"><tt class="xref docutils literal"><span class="pre">TestSuite</span></tt></a> object as a parameter, and
returns a result object.  The class <a title="unittest.TestResult" class="reference internal" href="#unittest.TestResult"><tt class="xref docutils literal"><span class="pre">TestResult</span></tt></a> is provided for use as
the result object. <tt class="xref docutils literal"><span class="pre">unittest</span></tt> provides the <a title="unittest.TextTestRunner" class="reference internal" href="#unittest.TextTestRunner"><tt class="xref docutils literal"><span class="pre">TextTestRunner</span></tt></a> as an
example test runner which reports test results on the standard error stream by
default.  Alternate runners can be implemented for other environments (such as
graphical environments) without any need to derive from a specific class.</p>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt>Module <a title="Test pieces of code within docstrings." class="reference external" href="doctest.html#module-doctest"><tt class="xref docutils literal"><span class="pre">doctest</span></tt></a></dt>
<dd>Another test-support module with a very different flavor.</dd>
<dt><a class="reference external" href="http://www.XProgramming.com/testfram.htm">Simple Smalltalk Testing: With Patterns</a></dt>
<dd>Kent Beck&#8217;s original paper on testing frameworks using the pattern shared by
<tt class="xref docutils literal"><span class="pre">unittest</span></tt>.</dd>
<dt><a class="reference external" href="http://code.google.com/p/python-nose/">Nose</a> and <a class="reference external" href="http://pytest.org">py.test</a></dt>
<dd>Third-party unittest frameworks with a lighter-weight syntax
for writing tests.  For example, <tt class="docutils literal"><span class="pre">assert</span> <span class="pre">func(10)</span> <span class="pre">==</span> <span class="pre">42</span></tt>.</dd>
<dt><a class="reference external" href="http://python-mock.sourceforge.net/">python-mock</a> and <a class="reference external" href="http://blog.ianbicking.org/minimock.html">minimock</a></dt>
<dd>Tools for creating mock test objects (objects simulating external resources).</dd>
</dl>
</div>
<div class="section" id="basic-example">
<span id="unittest-minimal-example"></span><h2>26.3.1. Basic example<a class="headerlink" href="#basic-example" title="Permalink to this headline">¶</a></h2>
<p>The <tt class="xref docutils literal"><span class="pre">unittest</span></tt> module provides a rich set of tools for constructing and
running tests.  This section demonstrates that a small subset of the tools
suffice to meet the needs of most users.</p>
<p>Here is a short script to test three functions from the <a title="Generate pseudo-random numbers with various common distributions." class="reference external" href="random.html#module-random"><tt class="xref docutils literal"><span class="pre">random</span></tt></a> module:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">random</span>
<span class="kn">import</span> <span class="nn">unittest</span>

<span class="k">class</span> <span class="nc">TestSequenceFunctions</span><span class="p">(</span><span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">setUp</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">seq</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="mf">10</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">testshuffle</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c"># make sure the shuffled sequence does not lose any elements</span>
        <span class="n">random</span><span class="o">.</span><span class="n">shuffle</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">seq</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">seq</span><span class="o">.</span><span class="n">sort</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">seq</span><span class="p">,</span> <span class="nb">range</span><span class="p">(</span><span class="mf">10</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">testchoice</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">element</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">choice</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">seq</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">assert_</span><span class="p">(</span><span class="n">element</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">seq</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">testsample</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">assertRaises</span><span class="p">(</span><span class="ne">ValueError</span><span class="p">,</span> <span class="n">random</span><span class="o">.</span><span class="n">sample</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">seq</span><span class="p">,</span> <span class="mf">20</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">element</span> <span class="ow">in</span> <span class="n">random</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">seq</span><span class="p">,</span> <span class="mf">5</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">assert_</span><span class="p">(</span><span class="n">element</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">seq</span><span class="p">)</span>

<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&#39;__main__&#39;</span><span class="p">:</span>
    <span class="n">unittest</span><span class="o">.</span><span class="n">main</span><span class="p">()</span>
</pre></div>
</div>
<p>A testcase is created by subclassing <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">unittest.TestCase</span></tt></a>. The three
individual tests are defined with methods whose names start with the letters
<tt class="docutils literal"><span class="pre">test</span></tt>.  This naming convention informs the test runner about which methods
represent tests.</p>
<p>The crux of each test is a call to <tt class="xref docutils literal"><span class="pre">assertEqual()</span></tt> to check for an expected
result; <tt class="xref docutils literal"><span class="pre">assert_()</span></tt> to verify a condition; or <tt class="xref docutils literal"><span class="pre">assertRaises()</span></tt> to verify
that an expected exception gets raised.  These methods are used instead of the
<a class="reference external" href="../reference/simple_stmts.html#assert"><tt class="xref docutils literal"><span class="pre">assert</span></tt></a> statement so the test runner can accumulate all test results
and produce a report.</p>
<p>When a <tt class="xref docutils literal"><span class="pre">setUp()</span></tt> method is defined, the test runner will run that method
prior to each test.  Likewise, if a <tt class="xref docutils literal"><span class="pre">tearDown()</span></tt> method is defined, the test
runner will invoke that method after each test.  In the example, <tt class="xref docutils literal"><span class="pre">setUp()</span></tt>
was used to create a fresh sequence for each test.</p>
<p>The final block shows a simple way to run the tests. <a title="unittest.main" class="reference internal" href="#unittest.main"><tt class="xref docutils literal"><span class="pre">unittest.main()</span></tt></a>
provides a command line interface to the test script.  When run from the command
line, the above script produces an output that looks like this:</p>
<div class="highlight-python"><pre>...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK</pre>
</div>
<p>Instead of <a title="unittest.main" class="reference internal" href="#unittest.main"><tt class="xref docutils literal"><span class="pre">unittest.main()</span></tt></a>, there are other ways to run the tests with a
finer level of control, less terse output, and no requirement to be run from the
command line.  For example, the last two lines may be replaced with:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">suite</span> <span class="o">=</span> <span class="n">unittest</span><span class="o">.</span><span class="n">TestLoader</span><span class="p">()</span><span class="o">.</span><span class="n">loadTestsFromTestCase</span><span class="p">(</span><span class="n">TestSequenceFunctions</span><span class="p">)</span>
<span class="n">unittest</span><span class="o">.</span><span class="n">TextTestRunner</span><span class="p">(</span><span class="n">verbosity</span><span class="o">=</span><span class="mf">2</span><span class="p">)</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">suite</span><span class="p">)</span>
</pre></div>
</div>
<p>Running the revised script from the interpreter or another script produces the
following output:</p>
<div class="highlight-python"><pre>testchoice (__main__.TestSequenceFunctions) ... ok
testsample (__main__.TestSequenceFunctions) ... ok
testshuffle (__main__.TestSequenceFunctions) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.110s

OK</pre>
</div>
<p>The above examples show the most commonly used <tt class="xref docutils literal"><span class="pre">unittest</span></tt> features which
are sufficient to meet many everyday testing needs.  The remainder of the
documentation explores the full feature set from first principles.</p>
</div>
<div class="section" id="organizing-test-code">
<span id="organizing-tests"></span><h2>26.3.2. Organizing test code<a class="headerlink" href="#organizing-test-code" title="Permalink to this headline">¶</a></h2>
<p>The basic building blocks of unit testing are <em>test cases</em> &#8212; single
scenarios that must be set up and checked for correctness.  In <tt class="xref docutils literal"><span class="pre">unittest</span></tt>,
test cases are represented by instances of <tt class="xref docutils literal"><span class="pre">unittest</span></tt>&#8216;s <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a>
class. To make your own test cases you must write subclasses of
<a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a>, or use <a title="unittest.FunctionTestCase" class="reference internal" href="#unittest.FunctionTestCase"><tt class="xref docutils literal"><span class="pre">FunctionTestCase</span></tt></a>.</p>
<p>An instance of a <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a>-derived class is an object that can
completely run a single test method, together with optional set-up and tidy-up
code.</p>
<p>The testing code of a <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> instance should be entirely self
contained, such that it can be run either in isolation or in arbitrary
combination with any number of other test cases.</p>
<p>The simplest <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> subclass will simply override the <tt class="xref docutils literal"><span class="pre">runTest()</span></tt>
method in order to perform specific testing code:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">unittest</span>

<span class="k">class</span> <span class="nc">DefaultWidgetSizeTestCase</span><span class="p">(</span><span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">runTest</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">widget</span> <span class="o">=</span> <span class="n">Widget</span><span class="p">(</span><span class="s">&#39;The widget&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="n">widget</span><span class="o">.</span><span class="n">size</span><span class="p">(),</span> <span class="p">(</span><span class="mf">50</span><span class="p">,</span> <span class="mf">50</span><span class="p">),</span> <span class="s">&#39;incorrect default size&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>Note that in order to test something, we use the one of the <tt class="xref docutils literal"><span class="pre">assert*()</span></tt> or
<tt class="xref docutils literal"><span class="pre">fail*()</span></tt> methods provided by the <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> base class.  If the
test fails, an exception will be raised, and <tt class="xref docutils literal"><span class="pre">unittest</span></tt> will identify the
test case as a <em>failure</em>.  Any other exceptions will be treated as
<em>errors</em>. This helps you identify where the problem is: <em>failures</em> are
caused by incorrect results - a 5 where you expected a 6. <em>Errors</em> are
caused by incorrect code - e.g., a <a title="exceptions.TypeError" class="reference external" href="exceptions.html#exceptions.TypeError"><tt class="xref docutils literal"><span class="pre">TypeError</span></tt></a> caused by an incorrect
function call.</p>
<p>The way to run a test case will be described later.  For now, note that to
construct an instance of such a test case, we call its constructor without
arguments:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">testCase</span> <span class="o">=</span> <span class="n">DefaultWidgetSizeTestCase</span><span class="p">()</span>
</pre></div>
</div>
<p>Now, such test cases can be numerous, and their set-up can be repetitive.  In
the above case, constructing a <tt class="xref docutils literal"><span class="pre">Widget</span></tt> in each of 100 Widget test case
subclasses would mean unsightly duplication.</p>
<p>Luckily, we can factor out such set-up code by implementing a method called
<tt class="xref docutils literal"><span class="pre">setUp()</span></tt>, which the testing framework will automatically call for us when
we run the test:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">unittest</span>

<span class="k">class</span> <span class="nc">SimpleWidgetTestCase</span><span class="p">(</span><span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">setUp</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">widget</span> <span class="o">=</span> <span class="n">Widget</span><span class="p">(</span><span class="s">&#39;The widget&#39;</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">DefaultWidgetSizeTestCase</span><span class="p">(</span><span class="n">SimpleWidgetTestCase</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">runTest</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">failUnless</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">widget</span><span class="o">.</span><span class="n">size</span><span class="p">()</span> <span class="o">==</span> <span class="p">(</span><span class="mf">50</span><span class="p">,</span><span class="mf">50</span><span class="p">),</span>
                        <span class="s">&#39;incorrect default size&#39;</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">WidgetResizeTestCase</span><span class="p">(</span><span class="n">SimpleWidgetTestCase</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">runTest</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">widget</span><span class="o">.</span><span class="n">resize</span><span class="p">(</span><span class="mf">100</span><span class="p">,</span><span class="mf">150</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">failUnless</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">widget</span><span class="o">.</span><span class="n">size</span><span class="p">()</span> <span class="o">==</span> <span class="p">(</span><span class="mf">100</span><span class="p">,</span><span class="mf">150</span><span class="p">),</span>
                        <span class="s">&#39;wrong size after resize&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>If the <tt class="xref docutils literal"><span class="pre">setUp()</span></tt> method raises an exception while the test is running, the
framework will consider the test to have suffered an error, and the
<tt class="xref docutils literal"><span class="pre">runTest()</span></tt> method will not be executed.</p>
<p>Similarly, we can provide a <tt class="xref docutils literal"><span class="pre">tearDown()</span></tt> method that tidies up after the
<tt class="xref docutils literal"><span class="pre">runTest()</span></tt> method has been run:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">unittest</span>

<span class="k">class</span> <span class="nc">SimpleWidgetTestCase</span><span class="p">(</span><span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">setUp</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">widget</span> <span class="o">=</span> <span class="n">Widget</span><span class="p">(</span><span class="s">&#39;The widget&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">tearDown</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">widget</span><span class="o">.</span><span class="n">dispose</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">widget</span> <span class="o">=</span> <span class="bp">None</span>
</pre></div>
</div>
<p>If <tt class="xref docutils literal"><span class="pre">setUp()</span></tt> succeeded, the <tt class="xref docutils literal"><span class="pre">tearDown()</span></tt> method will be run whether
<tt class="xref docutils literal"><span class="pre">runTest()</span></tt> succeeded or not.</p>
<p>Such a working environment for the testing code is called a <em>fixture</em>.</p>
<p>Often, many small test cases will use the same fixture.  In this case, we would
end up subclassing <tt class="xref docutils literal"><span class="pre">SimpleWidgetTestCase</span></tt> into many small one-method
classes such as <tt class="xref docutils literal"><span class="pre">DefaultWidgetSizeTestCase</span></tt>.  This is time-consuming and
discouraging, so in the same vein as JUnit, <tt class="xref docutils literal"><span class="pre">unittest</span></tt> provides a simpler
mechanism:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">unittest</span>

<span class="k">class</span> <span class="nc">WidgetTestCase</span><span class="p">(</span><span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">setUp</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">widget</span> <span class="o">=</span> <span class="n">Widget</span><span class="p">(</span><span class="s">&#39;The widget&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">tearDown</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">widget</span><span class="o">.</span><span class="n">dispose</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">widget</span> <span class="o">=</span> <span class="bp">None</span>

    <span class="k">def</span> <span class="nf">testDefaultSize</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">failUnless</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">widget</span><span class="o">.</span><span class="n">size</span><span class="p">()</span> <span class="o">==</span> <span class="p">(</span><span class="mf">50</span><span class="p">,</span><span class="mf">50</span><span class="p">),</span>
                        <span class="s">&#39;incorrect default size&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">testResize</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">widget</span><span class="o">.</span><span class="n">resize</span><span class="p">(</span><span class="mf">100</span><span class="p">,</span><span class="mf">150</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">failUnless</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">widget</span><span class="o">.</span><span class="n">size</span><span class="p">()</span> <span class="o">==</span> <span class="p">(</span><span class="mf">100</span><span class="p">,</span><span class="mf">150</span><span class="p">),</span>
                        <span class="s">&#39;wrong size after resize&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>Here we have not provided a <tt class="xref docutils literal"><span class="pre">runTest()</span></tt> method, but have instead provided
two different test methods.  Class instances will now each run one of the
<tt class="xref docutils literal"><span class="pre">test*()</span></tt>  methods, with <tt class="docutils literal"><span class="pre">self.widget</span></tt> created and destroyed separately
for each instance.  When creating an instance we must specify the test method it
is to run.  We do this by passing the method name in the constructor:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">defaultSizeTestCase</span> <span class="o">=</span> <span class="n">WidgetTestCase</span><span class="p">(</span><span class="s">&#39;testDefaultSize&#39;</span><span class="p">)</span>
<span class="n">resizeTestCase</span> <span class="o">=</span> <span class="n">WidgetTestCase</span><span class="p">(</span><span class="s">&#39;testResize&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>Test case instances are grouped together according to the features they test.
<tt class="xref docutils literal"><span class="pre">unittest</span></tt> provides a mechanism for this: the <em>test suite</em>,
represented by <tt class="xref docutils literal"><span class="pre">unittest</span></tt>&#8216;s <a title="unittest.TestSuite" class="reference internal" href="#unittest.TestSuite"><tt class="xref docutils literal"><span class="pre">TestSuite</span></tt></a> class:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">widgetTestSuite</span> <span class="o">=</span> <span class="n">unittest</span><span class="o">.</span><span class="n">TestSuite</span><span class="p">()</span>
<span class="n">widgetTestSuite</span><span class="o">.</span><span class="n">addTest</span><span class="p">(</span><span class="n">WidgetTestCase</span><span class="p">(</span><span class="s">&#39;testDefaultSize&#39;</span><span class="p">))</span>
<span class="n">widgetTestSuite</span><span class="o">.</span><span class="n">addTest</span><span class="p">(</span><span class="n">WidgetTestCase</span><span class="p">(</span><span class="s">&#39;testResize&#39;</span><span class="p">))</span>
</pre></div>
</div>
<p>For the ease of running tests, as we will see later, it is a good idea to
provide in each test module a callable object that returns a pre-built test
suite:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">suite</span><span class="p">():</span>
    <span class="n">suite</span> <span class="o">=</span> <span class="n">unittest</span><span class="o">.</span><span class="n">TestSuite</span><span class="p">()</span>
    <span class="n">suite</span><span class="o">.</span><span class="n">addTest</span><span class="p">(</span><span class="n">WidgetTestCase</span><span class="p">(</span><span class="s">&#39;testDefaultSize&#39;</span><span class="p">))</span>
    <span class="n">suite</span><span class="o">.</span><span class="n">addTest</span><span class="p">(</span><span class="n">WidgetTestCase</span><span class="p">(</span><span class="s">&#39;testResize&#39;</span><span class="p">))</span>
    <span class="k">return</span> <span class="n">suite</span>
</pre></div>
</div>
<p>or even:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">suite</span><span class="p">():</span>
    <span class="n">tests</span> <span class="o">=</span> <span class="p">[</span><span class="s">&#39;testDefaultSize&#39;</span><span class="p">,</span> <span class="s">&#39;testResize&#39;</span><span class="p">]</span>

    <span class="k">return</span> <span class="n">unittest</span><span class="o">.</span><span class="n">TestSuite</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="n">WidgetTestCase</span><span class="p">,</span> <span class="n">tests</span><span class="p">))</span>
</pre></div>
</div>
<p>Since it is a common pattern to create a <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> subclass with many
similarly named test functions, <tt class="xref docutils literal"><span class="pre">unittest</span></tt> provides a <a title="unittest.TestLoader" class="reference internal" href="#unittest.TestLoader"><tt class="xref docutils literal"><span class="pre">TestLoader</span></tt></a>
class that can be used to automate the process of creating a test suite and
populating it with individual tests. For example,</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">suite</span> <span class="o">=</span> <span class="n">unittest</span><span class="o">.</span><span class="n">TestLoader</span><span class="p">()</span><span class="o">.</span><span class="n">loadTestsFromTestCase</span><span class="p">(</span><span class="n">WidgetTestCase</span><span class="p">)</span>
</pre></div>
</div>
<p>will create a test suite that will run <tt class="docutils literal"><span class="pre">WidgetTestCase.testDefaultSize()</span></tt> and
<tt class="docutils literal"><span class="pre">WidgetTestCase.testResize</span></tt>. <a title="unittest.TestLoader" class="reference internal" href="#unittest.TestLoader"><tt class="xref docutils literal"><span class="pre">TestLoader</span></tt></a> uses the <tt class="docutils literal"><span class="pre">'test'</span></tt> method
name prefix to identify test methods automatically.</p>
<p>Note that the order in which the various test cases will be run is determined by
sorting the test function names with the built-in <a title="cmp" class="reference external" href="functions.html#cmp"><tt class="xref docutils literal"><span class="pre">cmp()</span></tt></a> function.</p>
<p>Often it is desirable to group suites of test cases together, so as to run tests
for the whole system at once.  This is easy, since <a title="unittest.TestSuite" class="reference internal" href="#unittest.TestSuite"><tt class="xref docutils literal"><span class="pre">TestSuite</span></tt></a> instances
can be added to a <a title="unittest.TestSuite" class="reference internal" href="#unittest.TestSuite"><tt class="xref docutils literal"><span class="pre">TestSuite</span></tt></a> just as <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> instances can be
added to a <a title="unittest.TestSuite" class="reference internal" href="#unittest.TestSuite"><tt class="xref docutils literal"><span class="pre">TestSuite</span></tt></a>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">suite1</span> <span class="o">=</span> <span class="n">module1</span><span class="o">.</span><span class="n">TheTestSuite</span><span class="p">()</span>
<span class="n">suite2</span> <span class="o">=</span> <span class="n">module2</span><span class="o">.</span><span class="n">TheTestSuite</span><span class="p">()</span>
<span class="n">alltests</span> <span class="o">=</span> <span class="n">unittest</span><span class="o">.</span><span class="n">TestSuite</span><span class="p">([</span><span class="n">suite1</span><span class="p">,</span> <span class="n">suite2</span><span class="p">])</span>
</pre></div>
</div>
<p>You can place the definitions of test cases and test suites in the same modules
as the code they are to test (such as <tt class="docutils literal"><span class="pre">widget.py</span></tt>), but there are several
advantages to placing the test code in a separate module, such as
<tt class="docutils literal"><span class="pre">test_widget.py</span></tt>:</p>
<ul class="simple">
<li>The test module can be run standalone from the command line.</li>
<li>The test code can more easily be separated from shipped code.</li>
<li>There is less temptation to change test code to fit the code it tests without
a good reason.</li>
<li>Test code should be modified much less frequently than the code it tests.</li>
<li>Tested code can be refactored more easily.</li>
<li>Tests for modules written in C must be in separate modules anyway, so why not
be consistent?</li>
<li>If the testing strategy changes, there is no need to change the source code.</li>
</ul>
</div>
<div class="section" id="re-using-old-test-code">
<span id="legacy-unit-tests"></span><h2>26.3.3. Re-using old test code<a class="headerlink" href="#re-using-old-test-code" title="Permalink to this headline">¶</a></h2>
<p>Some users will find that they have existing test code that they would like to
run from <tt class="xref docutils literal"><span class="pre">unittest</span></tt>, without converting every old test function to a
<a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> subclass.</p>
<p>For this reason, <tt class="xref docutils literal"><span class="pre">unittest</span></tt> provides a <a title="unittest.FunctionTestCase" class="reference internal" href="#unittest.FunctionTestCase"><tt class="xref docutils literal"><span class="pre">FunctionTestCase</span></tt></a> class.
This subclass of <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> can be used to wrap an existing test
function.  Set-up and tear-down functions can also be provided.</p>
<p>Given the following test function:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">testSomething</span><span class="p">():</span>
    <span class="n">something</span> <span class="o">=</span> <span class="n">makeSomething</span><span class="p">()</span>
    <span class="k">assert</span> <span class="n">something</span><span class="o">.</span><span class="n">name</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span>
    <span class="c"># ...</span>
</pre></div>
</div>
<p>one can create an equivalent test case instance as follows:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">testcase</span> <span class="o">=</span> <span class="n">unittest</span><span class="o">.</span><span class="n">FunctionTestCase</span><span class="p">(</span><span class="n">testSomething</span><span class="p">)</span>
</pre></div>
</div>
<p>If there are additional set-up and tear-down methods that should be called as
part of the test case&#8217;s operation, they can also be provided like so:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">testcase</span> <span class="o">=</span> <span class="n">unittest</span><span class="o">.</span><span class="n">FunctionTestCase</span><span class="p">(</span><span class="n">testSomething</span><span class="p">,</span>
                                     <span class="n">setUp</span><span class="o">=</span><span class="n">makeSomethingDB</span><span class="p">,</span>
                                     <span class="n">tearDown</span><span class="o">=</span><span class="n">deleteSomethingDB</span><span class="p">)</span>
</pre></div>
</div>
<p>To make migrating existing test suites easier, <tt class="xref docutils literal"><span class="pre">unittest</span></tt> supports tests
raising <a title="exceptions.AssertionError" class="reference external" href="exceptions.html#exceptions.AssertionError"><tt class="xref docutils literal"><span class="pre">AssertionError</span></tt></a> to indicate test failure. However, it is
recommended that you use the explicit <tt class="xref docutils literal"><span class="pre">TestCase.fail*()</span></tt> and
<tt class="xref docutils literal"><span class="pre">TestCase.assert*()</span></tt> methods instead, as future versions of <tt class="xref docutils literal"><span class="pre">unittest</span></tt>
may treat <a title="exceptions.AssertionError" class="reference external" href="exceptions.html#exceptions.AssertionError"><tt class="xref docutils literal"><span class="pre">AssertionError</span></tt></a> differently.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Even though <a title="unittest.FunctionTestCase" class="reference internal" href="#unittest.FunctionTestCase"><tt class="xref docutils literal"><span class="pre">FunctionTestCase</span></tt></a> can be used to quickly convert an existing
test base over to a <tt class="xref docutils literal"><span class="pre">unittest</span></tt>-based system, this approach is not
recommended.  Taking the time to set up proper <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> subclasses will
make future test refactorings infinitely easier.</p>
</div>
</div>
<div class="section" id="classes-and-functions">
<span id="unittest-contents"></span><h2>26.3.4. Classes and functions<a class="headerlink" href="#classes-and-functions" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="unittest.TestCase">
<em class="property">
class </em><tt class="descclassname">unittest.</tt><tt class="descname">TestCase</tt><big>(</big><span class="optional">[</span><em>methodName</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestCase" title="Permalink to this definition">¶</a></dt>
<dd><p>Instances of the <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> class represent the smallest testable units
in the <tt class="xref docutils literal"><span class="pre">unittest</span></tt> universe.  This class is intended to be used as a base
class, with specific tests being implemented by concrete subclasses.  This class
implements the interface needed by the test runner to allow it to drive the
test, and methods that the test code can use to check for and report various
kinds of failure.</p>
<p>Each instance of <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> will run a single test method: the method
named <em>methodName</em>.  If you remember, we had an earlier example that went
something like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">suite</span><span class="p">():</span>
    <span class="n">suite</span> <span class="o">=</span> <span class="n">unittest</span><span class="o">.</span><span class="n">TestSuite</span><span class="p">()</span>
    <span class="n">suite</span><span class="o">.</span><span class="n">addTest</span><span class="p">(</span><span class="n">WidgetTestCase</span><span class="p">(</span><span class="s">&#39;testDefaultSize&#39;</span><span class="p">))</span>
    <span class="n">suite</span><span class="o">.</span><span class="n">addTest</span><span class="p">(</span><span class="n">WidgetTestCase</span><span class="p">(</span><span class="s">&#39;testResize&#39;</span><span class="p">))</span>
    <span class="k">return</span> <span class="n">suite</span>
</pre></div>
</div>
<p>Here, we create two instances of <tt class="xref docutils literal"><span class="pre">WidgetTestCase</span></tt>, each of which runs a
single test.</p>
<p><em>methodName</em> defaults to <tt class="docutils literal"><span class="pre">'runTest'</span></tt>.</p>
</dd></dl>

<dl class="class">
<dt id="unittest.FunctionTestCase">
<em class="property">
class </em><tt class="descclassname">unittest.</tt><tt class="descname">FunctionTestCase</tt><big>(</big><em>testFunc</em><span class="optional">[</span>, <em>setUp</em><span class="optional">[</span>, <em>tearDown</em><span class="optional">[</span>, <em>description</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.FunctionTestCase" title="Permalink to this definition">¶</a></dt>
<dd>This class implements the portion of the <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> interface which
allows the test runner to drive the test, but does not provide the methods which
test code can use to check and report errors. This is used to create test cases
using legacy test code, allowing it to be integrated into a <tt class="xref docutils literal"><span class="pre">unittest</span></tt>-based test framework.</dd></dl>

<dl class="class">
<dt id="unittest.TestSuite">
<em class="property">
class </em><tt class="descclassname">unittest.</tt><tt class="descname">TestSuite</tt><big>(</big><span class="optional">[</span><em>tests</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestSuite" title="Permalink to this definition">¶</a></dt>
<dd><p>This class represents an aggregation of individual tests cases and test suites.
The class presents the interface needed by the test runner to allow it to be run
as any other test case.  Running a <a title="unittest.TestSuite" class="reference internal" href="#unittest.TestSuite"><tt class="xref docutils literal"><span class="pre">TestSuite</span></tt></a> instance is the same as
iterating over the suite, running each test individually.</p>
<p>If <em>tests</em> is given, it must be an iterable of individual test cases or other
test suites that will be used to build the suite initially. Additional methods
are provided to add test cases and suites to the collection later on.</p>
</dd></dl>

<dl class="class">
<dt id="unittest.TestLoader">
<em class="property">
class </em><tt class="descclassname">unittest.</tt><tt class="descname">TestLoader</tt><a class="headerlink" href="#unittest.TestLoader" title="Permalink to this definition">¶</a></dt>
<dd>This class is responsible for loading tests according to various criteria and
returning them wrapped in a <a title="unittest.TestSuite" class="reference internal" href="#unittest.TestSuite"><tt class="xref docutils literal"><span class="pre">TestSuite</span></tt></a>. It can load all tests within a
given module or <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> subclass.</dd></dl>

<dl class="class">
<dt id="unittest.TestResult">
<em class="property">
class </em><tt class="descclassname">unittest.</tt><tt class="descname">TestResult</tt><a class="headerlink" href="#unittest.TestResult" title="Permalink to this definition">¶</a></dt>
<dd>This class is used to compile information about which tests have succeeded and
which have failed.</dd></dl>

<dl class="data">
<dt id="unittest.defaultTestLoader">
<tt class="descclassname">unittest.</tt><tt class="descname">defaultTestLoader</tt><a class="headerlink" href="#unittest.defaultTestLoader" title="Permalink to this definition">¶</a></dt>
<dd>Instance of the <a title="unittest.TestLoader" class="reference internal" href="#unittest.TestLoader"><tt class="xref docutils literal"><span class="pre">TestLoader</span></tt></a> class intended to be shared.  If no
customization of the <a title="unittest.TestLoader" class="reference internal" href="#unittest.TestLoader"><tt class="xref docutils literal"><span class="pre">TestLoader</span></tt></a> is needed, this instance can be used
instead of repeatedly creating new instances.</dd></dl>

<dl class="class">
<dt id="unittest.TextTestRunner">
<em class="property">
class </em><tt class="descclassname">unittest.</tt><tt class="descname">TextTestRunner</tt><big>(</big><span class="optional">[</span><em>stream</em><span class="optional">[</span>, <em>descriptions</em><span class="optional">[</span>, <em>verbosity</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TextTestRunner" title="Permalink to this definition">¶</a></dt>
<dd>A basic test runner implementation which prints results on standard error.  It
has a few configurable parameters, but is essentially very simple.  Graphical
applications which run test suites should provide alternate implementations.</dd></dl>

<dl class="function">
<dt id="unittest.main">
<tt class="descclassname">unittest.</tt><tt class="descname">main</tt><big>(</big><span class="optional">[</span><em>module</em><span class="optional">[</span>, <em>defaultTest</em><span class="optional">[</span>, <em>argv</em><span class="optional">[</span>, <em>testRunner</em><span class="optional">[</span>, <em>testLoader</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.main" title="Permalink to this definition">¶</a></dt>
<dd><p>A command-line program that runs a set of tests; this is primarily for making
test modules conveniently executable.  The simplest use for this function is to
include the following line at the end of a test script:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&#39;__main__&#39;</span><span class="p">:</span>
    <span class="n">unittest</span><span class="o">.</span><span class="n">main</span><span class="p">()</span>
</pre></div>
</div>
<p>The <em>testRunner</em> argument can either be a test runner class or an already
created instance of it.</p>
</dd></dl>

<p>In some cases, the existing tests may have been written using the <a title="Test pieces of code within docstrings." class="reference external" href="doctest.html#module-doctest"><tt class="xref docutils literal"><span class="pre">doctest</span></tt></a>
module.  If so, that module provides a  <tt class="xref docutils literal"><span class="pre">DocTestSuite</span></tt> class that can
automatically build <a title="unittest.TestSuite" class="reference internal" href="#unittest.TestSuite"><tt class="xref docutils literal"><span class="pre">unittest.TestSuite</span></tt></a> instances from the existing
<a title="Test pieces of code within docstrings." class="reference external" href="doctest.html#module-doctest"><tt class="xref docutils literal"><span class="pre">doctest</span></tt></a>-based tests.</p>
<p>
<span class="versionmodified">New in version 2.3.</span></p>
</div>
<div class="section" id="testcase-objects">
<span id="id1"></span><h2>26.3.5. TestCase Objects<a class="headerlink" href="#testcase-objects" title="Permalink to this headline">¶</a></h2>
<p>Each <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> instance represents a single test, but each concrete
subclass may be used to define multiple tests &#8212; the concrete class represents
a single test fixture.  The fixture is created and cleaned up for each test
case.</p>
<p><a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> instances provide three groups of methods: one group used to
run the test, another used by the test implementation to check conditions and
report failures, and some inquiry methods allowing information about the test
itself to be gathered.</p>
<p>Methods in the first group (running the test) are:</p>
<dl class="method">
<dt id="unittest.TestCase.setUp">
<tt class="descclassname">TestCase.</tt><tt class="descname">setUp</tt><big>(</big><big>)</big><a class="headerlink" href="#unittest.TestCase.setUp" title="Permalink to this definition">¶</a></dt>
<dd>Method called to prepare the test fixture.  This is called immediately before
calling the test method; any exception raised by this method will be considered
an error rather than a test failure. The default implementation does nothing.</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.tearDown">
<tt class="descclassname">TestCase.</tt><tt class="descname">tearDown</tt><big>(</big><big>)</big><a class="headerlink" href="#unittest.TestCase.tearDown" title="Permalink to this definition">¶</a></dt>
<dd>Method called immediately after the test method has been called and the result
recorded.  This is called even if the test method raised an exception, so the
implementation in subclasses may need to be particularly careful about checking
internal state.  Any exception raised by this method will be considered an error
rather than a test failure.  This method will only be called if the
<a title="unittest.TestCase.setUp" class="reference internal" href="#unittest.TestCase.setUp"><tt class="xref docutils literal"><span class="pre">setUp()</span></tt></a> succeeds, regardless of the outcome of the test method. The
default implementation does nothing.</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.run">
<tt class="descclassname">TestCase.</tt><tt class="descname">run</tt><big>(</big><span class="optional">[</span><em>result</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestCase.run" title="Permalink to this definition">¶</a></dt>
<dd><p>Run the test, collecting the result into the test result object passed as
<em>result</em>.  If <em>result</em> is omitted or <a title="None" class="reference external" href="constants.html#None"><tt class="xref xref docutils literal"><span class="pre">None</span></tt></a>, a temporary result object is
created (by calling the <tt class="xref docutils literal"><span class="pre">defaultTestCase()</span></tt> method) and used; this result
object is not returned to <a title="unittest.TestCase.run" class="reference internal" href="#unittest.TestCase.run"><tt class="xref docutils literal"><span class="pre">run()</span></tt></a>&#8216;s caller.</p>
<p>The same effect may be had by simply calling the <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> instance.</p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.debug">
<tt class="descclassname">TestCase.</tt><tt class="descname">debug</tt><big>(</big><big>)</big><a class="headerlink" href="#unittest.TestCase.debug" title="Permalink to this definition">¶</a></dt>
<dd>Run the test without collecting the result.  This allows exceptions raised by
the test to be propagated to the caller, and can be used to support running
tests under a debugger.</dd></dl>

<p>The test code can use any of the following methods to check for and report
failures.</p>
<dl class="method">
<dt id="unittest.TestCase.assert_">
<tt class="descclassname">TestCase.</tt><tt class="descname">assert_</tt><big>(</big><em>expr</em><span class="optional">[</span>, <em>msg</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestCase.assert_" title="Permalink to this definition">¶</a></dt>
<dt id="unittest.TestCase.failUnless">
<tt class="descclassname">TestCase.</tt><tt class="descname">failUnless</tt><big>(</big><em>expr</em><span class="optional">[</span>, <em>msg</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestCase.failUnless" title="Permalink to this definition">¶</a></dt>
<dt id="unittest.TestCase.assertTrue">
<tt class="descclassname">TestCase.</tt><tt class="descname">assertTrue</tt><big>(</big><em>expr</em><span class="optional">[</span>, <em>msg</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestCase.assertTrue" title="Permalink to this definition">¶</a></dt>
<dd>Signal a test failure if <em>expr</em> is false; the explanation for the error will be
<em>msg</em> if given, otherwise it will be <a title="None" class="reference external" href="constants.html#None"><tt class="xref xref docutils literal"><span class="pre">None</span></tt></a>.</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.assertEqual">
<tt class="descclassname">TestCase.</tt><tt class="descname">assertEqual</tt><big>(</big><em>first</em>, <em>second</em><span class="optional">[</span>, <em>msg</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestCase.assertEqual" title="Permalink to this definition">¶</a></dt>
<dt id="unittest.TestCase.failUnlessEqual">
<tt class="descclassname">TestCase.</tt><tt class="descname">failUnlessEqual</tt><big>(</big><em>first</em>, <em>second</em><span class="optional">[</span>, <em>msg</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestCase.failUnlessEqual" title="Permalink to this definition">¶</a></dt>
<dd>Test that <em>first</em> and <em>second</em> are equal.  If the values do not compare equal,
the test will fail with the explanation given by <em>msg</em>, or <a title="None" class="reference external" href="constants.html#None"><tt class="xref xref docutils literal"><span class="pre">None</span></tt></a>.  Note
that using <a title="unittest.TestCase.failUnlessEqual" class="reference internal" href="#unittest.TestCase.failUnlessEqual"><tt class="xref docutils literal"><span class="pre">failUnlessEqual()</span></tt></a> improves upon doing the comparison as the
first parameter to <a title="unittest.TestCase.failUnless" class="reference internal" href="#unittest.TestCase.failUnless"><tt class="xref docutils literal"><span class="pre">failUnless()</span></tt></a>:  the default value for <em>msg</em> can be
computed to include representations of both <em>first</em> and <em>second</em>.</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.assertNotEqual">
<tt class="descclassname">TestCase.</tt><tt class="descname">assertNotEqual</tt><big>(</big><em>first</em>, <em>second</em><span class="optional">[</span>, <em>msg</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestCase.assertNotEqual" title="Permalink to this definition">¶</a></dt>
<dt id="unittest.TestCase.failIfEqual">
<tt class="descclassname">TestCase.</tt><tt class="descname">failIfEqual</tt><big>(</big><em>first</em>, <em>second</em><span class="optional">[</span>, <em>msg</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestCase.failIfEqual" title="Permalink to this definition">¶</a></dt>
<dd>Test that <em>first</em> and <em>second</em> are not equal.  If the values do compare equal,
the test will fail with the explanation given by <em>msg</em>, or <a title="None" class="reference external" href="constants.html#None"><tt class="xref xref docutils literal"><span class="pre">None</span></tt></a>.  Note
that using <a title="unittest.TestCase.failIfEqual" class="reference internal" href="#unittest.TestCase.failIfEqual"><tt class="xref docutils literal"><span class="pre">failIfEqual()</span></tt></a> improves upon doing the comparison as the first
parameter to <a title="unittest.TestCase.failUnless" class="reference internal" href="#unittest.TestCase.failUnless"><tt class="xref docutils literal"><span class="pre">failUnless()</span></tt></a> is that the default value for <em>msg</em> can be
computed to include representations of both <em>first</em> and <em>second</em>.</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.assertAlmostEqual">
<tt class="descclassname">TestCase.</tt><tt class="descname">assertAlmostEqual</tt><big>(</big><em>first</em>, <em>second</em><span class="optional">[</span>, <em>places</em><span class="optional">[</span>, <em>msg</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestCase.assertAlmostEqual" title="Permalink to this definition">¶</a></dt>
<dt id="unittest.TestCase.failUnlessAlmostEqual">
<tt class="descclassname">TestCase.</tt><tt class="descname">failUnlessAlmostEqual</tt><big>(</big><em>first</em>, <em>second</em><span class="optional">[</span>, <em>places</em><span class="optional">[</span>, <em>msg</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestCase.failUnlessAlmostEqual" title="Permalink to this definition">¶</a></dt>
<dd>Test that <em>first</em> and <em>second</em> are approximately equal by computing the
difference, rounding to the given number of decimal <em>places</em> (default 7),
and comparing to zero.
Note that comparing a given number of decimal places is not the same as
comparing a given number of significant digits. If the values do not compare
equal, the test will fail with the explanation given by <em>msg</em>, or <a title="None" class="reference external" href="constants.html#None"><tt class="xref xref docutils literal"><span class="pre">None</span></tt></a>.</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.assertNotAlmostEqual">
<tt class="descclassname">TestCase.</tt><tt class="descname">assertNotAlmostEqual</tt><big>(</big><em>first</em>, <em>second</em><span class="optional">[</span>, <em>places</em><span class="optional">[</span>, <em>msg</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestCase.assertNotAlmostEqual" title="Permalink to this definition">¶</a></dt>
<dt id="unittest.TestCase.failIfAlmostEqual">
<tt class="descclassname">TestCase.</tt><tt class="descname">failIfAlmostEqual</tt><big>(</big><em>first</em>, <em>second</em><span class="optional">[</span>, <em>places</em><span class="optional">[</span>, <em>msg</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestCase.failIfAlmostEqual" title="Permalink to this definition">¶</a></dt>
<dd>Test that <em>first</em> and <em>second</em> are not approximately equal by computing the
difference, rounding to the given number of decimal <em>places</em> (default 7),
and comparing to zero.
Note that comparing a given number of decimal places is not the same as
comparing a given number of significant digits. If the values do not compare
equal, the test will fail with the explanation given by <em>msg</em>, or <a title="None" class="reference external" href="constants.html#None"><tt class="xref xref docutils literal"><span class="pre">None</span></tt></a>.</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.assertRaises">
<tt class="descclassname">TestCase.</tt><tt class="descname">assertRaises</tt><big>(</big><em>exception</em>, <em>callable</em>, <em>...</em><big>)</big><a class="headerlink" href="#unittest.TestCase.assertRaises" title="Permalink to this definition">¶</a></dt>
<dt id="unittest.TestCase.failUnlessRaises">
<tt class="descclassname">TestCase.</tt><tt class="descname">failUnlessRaises</tt><big>(</big><em>exception</em>, <em>callable</em>, <em>...</em><big>)</big><a class="headerlink" href="#unittest.TestCase.failUnlessRaises" title="Permalink to this definition">¶</a></dt>
<dd>Test that an exception is raised when <em>callable</em> is called with any positional
or keyword arguments that are also passed to <a title="unittest.TestCase.assertRaises" class="reference internal" href="#unittest.TestCase.assertRaises"><tt class="xref docutils literal"><span class="pre">assertRaises()</span></tt></a>.  The test
passes if <em>exception</em> is raised, is an error if another exception is raised, or
fails if no exception is raised.  To catch any of a group of exceptions, a tuple
containing the exception classes may be passed as <em>exception</em>.</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.failIf">
<tt class="descclassname">TestCase.</tt><tt class="descname">failIf</tt><big>(</big><em>expr</em><span class="optional">[</span>, <em>msg</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestCase.failIf" title="Permalink to this definition">¶</a></dt>
<dt id="unittest.TestCase.assertFalse">
<tt class="descclassname">TestCase.</tt><tt class="descname">assertFalse</tt><big>(</big><em>expr</em><span class="optional">[</span>, <em>msg</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestCase.assertFalse" title="Permalink to this definition">¶</a></dt>
<dd>The inverse of the <a title="unittest.TestCase.failUnless" class="reference internal" href="#unittest.TestCase.failUnless"><tt class="xref docutils literal"><span class="pre">failUnless()</span></tt></a> method is the <a title="unittest.TestCase.failIf" class="reference internal" href="#unittest.TestCase.failIf"><tt class="xref docutils literal"><span class="pre">failIf()</span></tt></a> method.  This
signals a test failure if <em>expr</em> is true, with <em>msg</em> or <a title="None" class="reference external" href="constants.html#None"><tt class="xref xref docutils literal"><span class="pre">None</span></tt></a> for the
error message.</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.fail">
<tt class="descclassname">TestCase.</tt><tt class="descname">fail</tt><big>(</big><span class="optional">[</span><em>msg</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestCase.fail" title="Permalink to this definition">¶</a></dt>
<dd>Signals a test failure unconditionally, with <em>msg</em> or <a title="None" class="reference external" href="constants.html#None"><tt class="xref xref docutils literal"><span class="pre">None</span></tt></a> for the
error message.</dd></dl>

<dl class="attribute">
<dt id="unittest.TestCase.failureException">
<tt class="descclassname">TestCase.</tt><tt class="descname">failureException</tt><a class="headerlink" href="#unittest.TestCase.failureException" title="Permalink to this definition">¶</a></dt>
<dd>This class attribute gives the exception raised by the <tt class="xref docutils literal"><span class="pre">test()</span></tt> method.  If
a test framework needs to use a specialized exception, possibly to carry
additional information, it must subclass this exception in order to &#8220;play fair&#8221;
with the framework.  The initial value of this attribute is
<a title="exceptions.AssertionError" class="reference external" href="exceptions.html#exceptions.AssertionError"><tt class="xref docutils literal"><span class="pre">AssertionError</span></tt></a>.</dd></dl>

<p>Testing frameworks can use the following methods to collect information on the
test:</p>
<dl class="method">
<dt id="unittest.TestCase.countTestCases">
<tt class="descclassname">TestCase.</tt><tt class="descname">countTestCases</tt><big>(</big><big>)</big><a class="headerlink" href="#unittest.TestCase.countTestCases" title="Permalink to this definition">¶</a></dt>
<dd>Return the number of tests represented by this test object.  For
<a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> instances, this will always be <tt class="docutils literal"><span class="pre">1</span></tt>.</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.defaultTestResult">
<tt class="descclassname">TestCase.</tt><tt class="descname">defaultTestResult</tt><big>(</big><big>)</big><a class="headerlink" href="#unittest.TestCase.defaultTestResult" title="Permalink to this definition">¶</a></dt>
<dd><p>Return an instance of the test result class that should be used for this test
case class (if no other result instance is provided to the <a title="unittest.TestCase.run" class="reference internal" href="#unittest.TestCase.run"><tt class="xref docutils literal"><span class="pre">run()</span></tt></a> method).</p>
<p>For <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> instances, this will always be an instance of
<a title="unittest.TestResult" class="reference internal" href="#unittest.TestResult"><tt class="xref docutils literal"><span class="pre">TestResult</span></tt></a>;  subclasses of <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> should override this as
necessary.</p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.id">
<tt class="descclassname">TestCase.</tt><tt class="descname">id</tt><big>(</big><big>)</big><a class="headerlink" href="#unittest.TestCase.id" title="Permalink to this definition">¶</a></dt>
<dd>Return a string identifying the specific test case.  This is usually the full
name of the test method, including the module and class name.</dd></dl>

<dl class="method">
<dt id="unittest.TestCase.shortDescription">
<tt class="descclassname">TestCase.</tt><tt class="descname">shortDescription</tt><big>(</big><big>)</big><a class="headerlink" href="#unittest.TestCase.shortDescription" title="Permalink to this definition">¶</a></dt>
<dd>Returns a one-line description of the test, or <a title="None" class="reference external" href="constants.html#None"><tt class="xref xref docutils literal"><span class="pre">None</span></tt></a> if no description
has been provided.  The default implementation of this method returns the first
line of the test method&#8217;s docstring, if available, or <a title="None" class="reference external" href="constants.html#None"><tt class="xref xref docutils literal"><span class="pre">None</span></tt></a>.</dd></dl>

</div>
<div class="section" id="testsuite-objects">
<span id="id2"></span><h2>26.3.6. TestSuite Objects<a class="headerlink" href="#testsuite-objects" title="Permalink to this headline">¶</a></h2>
<p><a title="unittest.TestSuite" class="reference internal" href="#unittest.TestSuite"><tt class="xref docutils literal"><span class="pre">TestSuite</span></tt></a> objects behave much like <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> objects, except
they do not actually implement a test.  Instead, they are used to aggregate
tests into groups of tests that should be run together. Some additional methods
are available to add tests to <a title="unittest.TestSuite" class="reference internal" href="#unittest.TestSuite"><tt class="xref docutils literal"><span class="pre">TestSuite</span></tt></a> instances:</p>
<dl class="method">
<dt id="unittest.TestSuite.addTest">
<tt class="descclassname">TestSuite.</tt><tt class="descname">addTest</tt><big>(</big><em>test</em><big>)</big><a class="headerlink" href="#unittest.TestSuite.addTest" title="Permalink to this definition">¶</a></dt>
<dd>Add a <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> or <a title="unittest.TestSuite" class="reference internal" href="#unittest.TestSuite"><tt class="xref docutils literal"><span class="pre">TestSuite</span></tt></a> to the suite.</dd></dl>

<dl class="method">
<dt id="unittest.TestSuite.addTests">
<tt class="descclassname">TestSuite.</tt><tt class="descname">addTests</tt><big>(</big><em>tests</em><big>)</big><a class="headerlink" href="#unittest.TestSuite.addTests" title="Permalink to this definition">¶</a></dt>
<dd><p>Add all the tests from an iterable of <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> and <a title="unittest.TestSuite" class="reference internal" href="#unittest.TestSuite"><tt class="xref docutils literal"><span class="pre">TestSuite</span></tt></a>
instances to this test suite.</p>
<p>This is equivalent to iterating over <em>tests</em>, calling <a title="unittest.TestSuite.addTest" class="reference internal" href="#unittest.TestSuite.addTest"><tt class="xref docutils literal"><span class="pre">addTest()</span></tt></a> for each
element.</p>
</dd></dl>

<p><a title="unittest.TestSuite" class="reference internal" href="#unittest.TestSuite"><tt class="xref docutils literal"><span class="pre">TestSuite</span></tt></a> shares the following methods with <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a>:</p>
<dl class="method">
<dt id="unittest.TestSuite.run">
<tt class="descclassname">TestSuite.</tt><tt class="descname">run</tt><big>(</big><em>result</em><big>)</big><a class="headerlink" href="#unittest.TestSuite.run" title="Permalink to this definition">¶</a></dt>
<dd>Run the tests associated with this suite, collecting the result into the test
result object passed as <em>result</em>.  Note that unlike <a title="unittest.TestCase.run" class="reference internal" href="#unittest.TestCase.run"><tt class="xref docutils literal"><span class="pre">TestCase.run()</span></tt></a>,
<a title="unittest.TestSuite.run" class="reference internal" href="#unittest.TestSuite.run"><tt class="xref docutils literal"><span class="pre">TestSuite.run()</span></tt></a> requires the result object to be passed in.</dd></dl>

<dl class="method">
<dt id="unittest.TestSuite.debug">
<tt class="descclassname">TestSuite.</tt><tt class="descname">debug</tt><big>(</big><big>)</big><a class="headerlink" href="#unittest.TestSuite.debug" title="Permalink to this definition">¶</a></dt>
<dd>Run the tests associated with this suite without collecting the result. This
allows exceptions raised by the test to be propagated to the caller and can be
used to support running tests under a debugger.</dd></dl>

<dl class="method">
<dt id="unittest.TestSuite.countTestCases">
<tt class="descclassname">TestSuite.</tt><tt class="descname">countTestCases</tt><big>(</big><big>)</big><a class="headerlink" href="#unittest.TestSuite.countTestCases" title="Permalink to this definition">¶</a></dt>
<dd>Return the number of tests represented by this test object, including all
individual tests and sub-suites.</dd></dl>

<p>In the typical usage of a <a title="unittest.TestSuite" class="reference internal" href="#unittest.TestSuite"><tt class="xref docutils literal"><span class="pre">TestSuite</span></tt></a> object, the <tt class="xref docutils literal"><span class="pre">run()</span></tt> method is
invoked by a <tt class="xref docutils literal"><span class="pre">TestRunner</span></tt> rather than by the end-user test harness.</p>
</div>
<div class="section" id="testresult-objects">
<span id="id3"></span><h2>26.3.7. TestResult Objects<a class="headerlink" href="#testresult-objects" title="Permalink to this headline">¶</a></h2>
<p>A <a title="unittest.TestResult" class="reference internal" href="#unittest.TestResult"><tt class="xref docutils literal"><span class="pre">TestResult</span></tt></a> object stores the results of a set of tests.  The
<a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> and <a title="unittest.TestSuite" class="reference internal" href="#unittest.TestSuite"><tt class="xref docutils literal"><span class="pre">TestSuite</span></tt></a> classes ensure that results are
properly recorded; test authors do not need to worry about recording the outcome
of tests.</p>
<p>Testing frameworks built on top of <tt class="xref docutils literal"><span class="pre">unittest</span></tt> may want access to the
<a title="unittest.TestResult" class="reference internal" href="#unittest.TestResult"><tt class="xref docutils literal"><span class="pre">TestResult</span></tt></a> object generated by running a set of tests for reporting
purposes; a <a title="unittest.TestResult" class="reference internal" href="#unittest.TestResult"><tt class="xref docutils literal"><span class="pre">TestResult</span></tt></a> instance is returned by the
<tt class="xref docutils literal"><span class="pre">TestRunner.run()</span></tt> method for this purpose.</p>
<p><a title="unittest.TestResult" class="reference internal" href="#unittest.TestResult"><tt class="xref docutils literal"><span class="pre">TestResult</span></tt></a> instances have the following attributes that will be of
interest when inspecting the results of running a set of tests:</p>
<dl class="attribute">
<dt id="unittest.TestResult.errors">
<tt class="descclassname">TestResult.</tt><tt class="descname">errors</tt><a class="headerlink" href="#unittest.TestResult.errors" title="Permalink to this definition">¶</a></dt>
<dd><p>A list containing 2-tuples of <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> instances and strings holding
formatted tracebacks. Each tuple represents a test which raised an unexpected
exception.</p>
<p>
<span class="versionmodified">Changed in version 2.2: </span>Contains formatted tracebacks instead of <a title="sys.exc_info" class="reference external" href="sys.html#sys.exc_info"><tt class="xref docutils literal"><span class="pre">sys.exc_info()</span></tt></a> results.</p>
</dd></dl>

<dl class="attribute">
<dt id="unittest.TestResult.failures">
<tt class="descclassname">TestResult.</tt><tt class="descname">failures</tt><a class="headerlink" href="#unittest.TestResult.failures" title="Permalink to this definition">¶</a></dt>
<dd><p>A list containing 2-tuples of <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> instances and strings holding
formatted tracebacks. Each tuple represents a test where a failure was
explicitly signalled using the <tt class="xref docutils literal"><span class="pre">TestCase.fail*()</span></tt> or
<tt class="xref docutils literal"><span class="pre">TestCase.assert*()</span></tt> methods.</p>
<p>
<span class="versionmodified">Changed in version 2.2: </span>Contains formatted tracebacks instead of <a title="sys.exc_info" class="reference external" href="sys.html#sys.exc_info"><tt class="xref docutils literal"><span class="pre">sys.exc_info()</span></tt></a> results.</p>
</dd></dl>

<dl class="attribute">
<dt id="unittest.TestResult.testsRun">
<tt class="descclassname">TestResult.</tt><tt class="descname">testsRun</tt><a class="headerlink" href="#unittest.TestResult.testsRun" title="Permalink to this definition">¶</a></dt>
<dd>The total number of tests run so far.</dd></dl>

<dl class="method">
<dt id="unittest.TestResult.wasSuccessful">
<tt class="descclassname">TestResult.</tt><tt class="descname">wasSuccessful</tt><big>(</big><big>)</big><a class="headerlink" href="#unittest.TestResult.wasSuccessful" title="Permalink to this definition">¶</a></dt>
<dd>Returns <a title="True" class="reference external" href="constants.html#True"><tt class="xref xref docutils literal"><span class="pre">True</span></tt></a> if all tests run so far have passed, otherwise returns
<a title="False" class="reference external" href="constants.html#False"><tt class="xref xref docutils literal"><span class="pre">False</span></tt></a>.</dd></dl>

<dl class="method">
<dt id="unittest.TestResult.stop">
<tt class="descclassname">TestResult.</tt><tt class="descname">stop</tt><big>(</big><big>)</big><a class="headerlink" href="#unittest.TestResult.stop" title="Permalink to this definition">¶</a></dt>
<dd><p>This method can be called to signal that the set of tests being run should be
aborted by setting the <a title="unittest.TestResult" class="reference internal" href="#unittest.TestResult"><tt class="xref docutils literal"><span class="pre">TestResult</span></tt></a>&#8216;s <tt class="docutils literal"><span class="pre">shouldStop</span></tt> attribute to
<a title="True" class="reference external" href="constants.html#True"><tt class="xref xref docutils literal"><span class="pre">True</span></tt></a>.  <tt class="xref docutils literal"><span class="pre">TestRunner</span></tt> objects should respect this flag and return
without running any additional tests.</p>
<p>For example, this feature is used by the <a title="unittest.TextTestRunner" class="reference internal" href="#unittest.TextTestRunner"><tt class="xref docutils literal"><span class="pre">TextTestRunner</span></tt></a> class to stop
the test framework when the user signals an interrupt from the keyboard.
Interactive tools which provide <tt class="xref docutils literal"><span class="pre">TestRunner</span></tt> implementations can use this
in a similar manner.</p>
</dd></dl>

<p>The following methods of the <a title="unittest.TestResult" class="reference internal" href="#unittest.TestResult"><tt class="xref docutils literal"><span class="pre">TestResult</span></tt></a> class are used to maintain the
internal data structures, and may be extended in subclasses to support
additional reporting requirements.  This is particularly useful in building
tools which support interactive reporting while tests are being run.</p>
<dl class="method">
<dt id="unittest.TestResult.startTest">
<tt class="descclassname">TestResult.</tt><tt class="descname">startTest</tt><big>(</big><em>test</em><big>)</big><a class="headerlink" href="#unittest.TestResult.startTest" title="Permalink to this definition">¶</a></dt>
<dd><p>Called when the test case <em>test</em> is about to be run.</p>
<p>The default implementation simply increments the instance&#8217;s <tt class="docutils literal"><span class="pre">testsRun</span></tt>
counter.</p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestResult.stopTest">
<tt class="descclassname">TestResult.</tt><tt class="descname">stopTest</tt><big>(</big><em>test</em><big>)</big><a class="headerlink" href="#unittest.TestResult.stopTest" title="Permalink to this definition">¶</a></dt>
<dd><p>Called after the test case <em>test</em> has been executed, regardless of the outcome.</p>
<p>The default implementation does nothing.</p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestResult.addError">
<tt class="descclassname">TestResult.</tt><tt class="descname">addError</tt><big>(</big><em>test</em>, <em>err</em><big>)</big><a class="headerlink" href="#unittest.TestResult.addError" title="Permalink to this definition">¶</a></dt>
<dd><p>Called when the test case <em>test</em> raises an unexpected exception <em>err</em> is a tuple
of the form returned by <a title="sys.exc_info" class="reference external" href="sys.html#sys.exc_info"><tt class="xref docutils literal"><span class="pre">sys.exc_info()</span></tt></a>: <tt class="docutils literal"><span class="pre">(type,</span> <span class="pre">value,</span> <span class="pre">traceback)</span></tt>.</p>
<p>The default implementation appends a tuple <tt class="docutils literal"><span class="pre">(test,</span> <span class="pre">formatted_err)</span></tt> to the
instance&#8217;s <tt class="docutils literal"><span class="pre">errors</span></tt> attribute, where <em>formatted_err</em> is a formatted
traceback derived from <em>err</em>.</p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestResult.addFailure">
<tt class="descclassname">TestResult.</tt><tt class="descname">addFailure</tt><big>(</big><em>test</em>, <em>err</em><big>)</big><a class="headerlink" href="#unittest.TestResult.addFailure" title="Permalink to this definition">¶</a></dt>
<dd><p>Called when the test case <em>test</em> signals a failure. <em>err</em> is a tuple of the form
returned by <a title="sys.exc_info" class="reference external" href="sys.html#sys.exc_info"><tt class="xref docutils literal"><span class="pre">sys.exc_info()</span></tt></a>:  <tt class="docutils literal"><span class="pre">(type,</span> <span class="pre">value,</span> <span class="pre">traceback)</span></tt>.</p>
<p>The default implementation appends a tuple <tt class="docutils literal"><span class="pre">(test,</span> <span class="pre">formatted_err)</span></tt> to the
instance&#8217;s <tt class="docutils literal"><span class="pre">failures</span></tt> attribute, where <em>formatted_err</em> is a formatted
traceback derived from <em>err</em>.</p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestResult.addSuccess">
<tt class="descclassname">TestResult.</tt><tt class="descname">addSuccess</tt><big>(</big><em>test</em><big>)</big><a class="headerlink" href="#unittest.TestResult.addSuccess" title="Permalink to this definition">¶</a></dt>
<dd><p>Called when the test case <em>test</em> succeeds.</p>
<p>The default implementation does nothing.</p>
</dd></dl>

</div>
<div class="section" id="testloader-objects">
<span id="id4"></span><h2>26.3.8. TestLoader Objects<a class="headerlink" href="#testloader-objects" title="Permalink to this headline">¶</a></h2>
<p>The <a title="unittest.TestLoader" class="reference internal" href="#unittest.TestLoader"><tt class="xref docutils literal"><span class="pre">TestLoader</span></tt></a> class is used to create test suites from classes and
modules.  Normally, there is no need to create an instance of this class; the
<tt class="xref docutils literal"><span class="pre">unittest</span></tt> module provides an instance that can be shared as
<tt class="docutils literal"><span class="pre">unittest.defaultTestLoader</span></tt>. Using a subclass or instance, however, allows
customization of some configurable properties.</p>
<p><a title="unittest.TestLoader" class="reference internal" href="#unittest.TestLoader"><tt class="xref docutils literal"><span class="pre">TestLoader</span></tt></a> objects have the following methods:</p>
<dl class="method">
<dt id="unittest.TestLoader.loadTestsFromTestCase">
<tt class="descclassname">TestLoader.</tt><tt class="descname">loadTestsFromTestCase</tt><big>(</big><em>testCaseClass</em><big>)</big><a class="headerlink" href="#unittest.TestLoader.loadTestsFromTestCase" title="Permalink to this definition">¶</a></dt>
<dd>Return a suite of all tests cases contained in the <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a>-derived
<tt class="xref docutils literal"><span class="pre">testCaseClass</span></tt>.</dd></dl>

<dl class="method">
<dt id="unittest.TestLoader.loadTestsFromModule">
<tt class="descclassname">TestLoader.</tt><tt class="descname">loadTestsFromModule</tt><big>(</big><em>module</em><big>)</big><a class="headerlink" href="#unittest.TestLoader.loadTestsFromModule" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a suite of all tests cases contained in the given module. This method
searches <em>module</em> for classes derived from <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> and creates an
instance of the class for each test method defined for the class.</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">While using a hierarchy of <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a>-derived classes can be convenient
in sharing fixtures and helper functions, defining test methods on base classes
that are not intended to be instantiated directly does not play well with this
method.  Doing so, however, can be useful when the fixtures are different and
defined in subclasses.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="unittest.TestLoader.loadTestsFromName">
<tt class="descclassname">TestLoader.</tt><tt class="descname">loadTestsFromName</tt><big>(</big><em>name</em><span class="optional">[</span>, <em>module</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestLoader.loadTestsFromName" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a suite of all tests cases given a string specifier.</p>
<p>The specifier <em>name</em> is a &#8220;dotted name&#8221; that may resolve either to a module, a
test case class, a test method within a test case class, a <a title="unittest.TestSuite" class="reference internal" href="#unittest.TestSuite"><tt class="xref docutils literal"><span class="pre">TestSuite</span></tt></a>
instance, or a callable object which returns a <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a> or
<a title="unittest.TestSuite" class="reference internal" href="#unittest.TestSuite"><tt class="xref docutils literal"><span class="pre">TestSuite</span></tt></a> instance.  These checks are applied in the order listed here;
that is, a method on a possible test case class will be picked up as &#8220;a test
method within a test case class&#8221;, rather than &#8220;a callable object&#8221;.</p>
<p>For example, if you have a module <tt class="xref docutils literal"><span class="pre">SampleTests</span></tt> containing a
<a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a>-derived class <tt class="xref docutils literal"><span class="pre">SampleTestCase</span></tt> with three test
methods (<tt class="xref docutils literal"><span class="pre">test_one()</span></tt>, <tt class="xref docutils literal"><span class="pre">test_two()</span></tt>, and <tt class="xref docutils literal"><span class="pre">test_three()</span></tt>), the
specifier <tt class="docutils literal"><span class="pre">'SampleTests.SampleTestCase'</span></tt> would cause this method to return a
suite which will run all three test methods.  Using the specifier
<tt class="docutils literal"><span class="pre">'SampleTests.SampleTestCase.test_two'</span></tt> would cause it to return a test suite
which will run only the <tt class="xref docutils literal"><span class="pre">test_two()</span></tt> test method.  The specifier can refer
to modules and packages which have not been imported; they will be imported as a
side-effect.</p>
<p>The method optionally resolves <em>name</em> relative to the given <em>module</em>.</p>
</dd></dl>

<dl class="method">
<dt id="unittest.TestLoader.loadTestsFromNames">
<tt class="descclassname">TestLoader.</tt><tt class="descname">loadTestsFromNames</tt><big>(</big><em>names</em><span class="optional">[</span>, <em>module</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#unittest.TestLoader.loadTestsFromNames" title="Permalink to this definition">¶</a></dt>
<dd>Similar to <a title="unittest.TestLoader.loadTestsFromName" class="reference internal" href="#unittest.TestLoader.loadTestsFromName"><tt class="xref docutils literal"><span class="pre">loadTestsFromName()</span></tt></a>, but takes a sequence of names rather than
a single name.  The return value is a test suite which supports all the tests
defined for each name.</dd></dl>

<dl class="method">
<dt id="unittest.TestLoader.getTestCaseNames">
<tt class="descclassname">TestLoader.</tt><tt class="descname">getTestCaseNames</tt><big>(</big><em>testCaseClass</em><big>)</big><a class="headerlink" href="#unittest.TestLoader.getTestCaseNames" title="Permalink to this definition">¶</a></dt>
<dd>Return a sorted sequence of method names found within <em>testCaseClass</em>; this
should be a subclass of <a title="unittest.TestCase" class="reference internal" href="#unittest.TestCase"><tt class="xref docutils literal"><span class="pre">TestCase</span></tt></a>.</dd></dl>

<p>The following attributes of a <a title="unittest.TestLoader" class="reference internal" href="#unittest.TestLoader"><tt class="xref docutils literal"><span class="pre">TestLoader</span></tt></a> can be configured either by
subclassing or assignment on an instance:</p>
<dl class="attribute">
<dt id="unittest.TestLoader.testMethodPrefix">
<tt class="descclassname">TestLoader.</tt><tt class="descname">testMethodPrefix</tt><a class="headerlink" href="#unittest.TestLoader.testMethodPrefix" title="Permalink to this definition">¶</a></dt>
<dd><p>String giving the prefix of method names which will be interpreted as test
methods.  The default value is <tt class="docutils literal"><span class="pre">'test'</span></tt>.</p>
<p>This affects <a title="unittest.TestLoader.getTestCaseNames" class="reference internal" href="#unittest.TestLoader.getTestCaseNames"><tt class="xref docutils literal"><span class="pre">getTestCaseNames()</span></tt></a> and all the <tt class="xref docutils literal"><span class="pre">loadTestsFrom*()</span></tt>
methods.</p>
</dd></dl>

<dl class="attribute">
<dt id="unittest.TestLoader.sortTestMethodsUsing">
<tt class="descclassname">TestLoader.</tt><tt class="descname">sortTestMethodsUsing</tt><a class="headerlink" href="#unittest.TestLoader.sortTestMethodsUsing" title="Permalink to this definition">¶</a></dt>
<dd>Function to be used to compare method names when sorting them in
<a title="unittest.TestLoader.getTestCaseNames" class="reference internal" href="#unittest.TestLoader.getTestCaseNames"><tt class="xref docutils literal"><span class="pre">getTestCaseNames()</span></tt></a> and all the <tt class="xref docutils literal"><span class="pre">loadTestsFrom*()</span></tt> methods. The
default value is the built-in <a title="cmp" class="reference external" href="functions.html#cmp"><tt class="xref docutils literal"><span class="pre">cmp()</span></tt></a> function; the attribute can also be
set to <a title="None" class="reference external" href="constants.html#None"><tt class="xref xref docutils literal"><span class="pre">None</span></tt></a> to disable the sort.</dd></dl>

<dl class="attribute">
<dt id="unittest.TestLoader.suiteClass">
<tt class="descclassname">TestLoader.</tt><tt class="descname">suiteClass</tt><a class="headerlink" href="#unittest.TestLoader.suiteClass" title="Permalink to this definition">¶</a></dt>
<dd><p>Callable object that constructs a test suite from a list of tests. No methods on
the resulting object are needed.  The default value is the <a title="unittest.TestSuite" class="reference internal" href="#unittest.TestSuite"><tt class="xref docutils literal"><span class="pre">TestSuite</span></tt></a>
class.</p>
<p>This affects all the <tt class="xref docutils literal"><span class="pre">loadTestsFrom*()</span></tt> methods.</p>
</dd></dl>

</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="../contents.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="">26.3. <tt class="docutils literal"><span class="pre">unittest</span></tt> &#8212; Unit testing framework</a><ul>
<li><a class="reference external" href="#basic-example">26.3.1. Basic example</a></li>
<li><a class="reference external" href="#organizing-test-code">26.3.2. Organizing test code</a></li>
<li><a class="reference external" href="#re-using-old-test-code">26.3.3. Re-using old test code</a></li>
<li><a class="reference external" href="#classes-and-functions">26.3.4. Classes and functions</a></li>
<li><a class="reference external" href="#testcase-objects">26.3.5. TestCase Objects</a></li>
<li><a class="reference external" href="#testsuite-objects">26.3.6. TestSuite Objects</a></li>
<li><a class="reference external" href="#testresult-objects">26.3.7. TestResult Objects</a></li>
<li><a class="reference external" href="#testloader-objects">26.3.8. TestLoader Objects</a></li>
</ul>
</li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="doctest.html"
                                  title="previous chapter">26.2. <tt class="docutils literal docutils literal docutils literal"><span class="pre">doctest</span></tt> &#8212; Test interactive Python examples</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="2to3.html"
                                  title="next chapter">26.4. 2to3 - Automated Python 2 to 3 code translation</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/library/unittest.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="2to3.html" title="26.4. 2to3 - Automated Python 2 to 3 code translation"
             >next</a> |</li>
        <li class="right" >
          <a href="doctest.html" title="26.2. doctest — Test interactive Python examples"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="development.html" >26. Development Tools</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; <a href="../copyright.html">Copyright</a> 1990-2009, Python Software Foundation.
      Last updated on Apr 15, 2009.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.1.
    </div>
  </body>
</html>